﻿using SafeLoad.NControl.Assist;
using System;
using System.Linq;
using System.Windows;

namespace SafeLoad.NControl
{
  /// <summary>
  /// 整个组件的代码辅助工具，提供了一个基础的类库方法
  /// </summary>
  public class NHelper
  {
    /// <summary>
    /// 计算绘图时的相对偏移值
    /// </summary>
    /// <param name="max">0-100分的最大值，就是指准备绘制的最大值</param>
    /// <param name="min">0-100分的最小值，就是指准备绘制的最小值</param>
    /// <param name="height">实际绘图区域的高度</param>
    /// <param name="value">需要绘制数据的当前值</param>
    /// <returns>相对于0的位置，还需要增加上面的偏值</returns>
    public static double ComputePaintLocationY(double max, double min, double height, double value)
    {
      if ((max - min) == 0f)
      {
        return height;
      }
      else
      {
        return height - (value - min) / (max - min) * height;
      }
    }


    /// <summary>
    /// 一个通用的数组新增个数方法，会自动判断越界情况，越界的情况下，会自动的截断或是填充 -> 
    /// A common array of new methods, will automatically determine the cross-border situation, in the case of cross-border, will be automatically truncated or filled
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    /// <param name="array">原数据</param>
    /// <param name="data">等待新增的数据</param>
    /// <param name="max">原数据的最大值</param>
    public static void AddArrayData<T>(ref T[] array, T[] data, int max)
    {
      if (data == null) return;           // 数据为空
      if (data.Length == 0) return;       // 数据长度为空

      if (array.Length == max)
      {
        Array.Copy(array, data.Length, array, 0, array.Length - data.Length);
        Array.Copy(data, 0, array, array.Length - data.Length, data.Length);
      }
      else
      {
        if ((array.Length + data.Length) > max)
        {
          T[] tmp = new T[max];
          for (int i = 0; i < (max - data.Length); i++)
          {
            tmp[i] = array[i + (array.Length - max + data.Length)];
          }
          for (int i = 0; i < data.Length; i++)
          {
            tmp[tmp.Length - data.Length + i] = data[i];
          }
          // 更新数据
          array = tmp;
        }
        else
        {
          T[] tmp = new T[array.Length + data.Length];
          for (int i = 0; i < array.Length; i++)
          {
            tmp[i] = array[i];
          }
          for (int i = 0; i < data.Length; i++)
          {
            tmp[tmp.Length - data.Length + i] = data[i];
          }

          array = tmp;
        }
      }
    }


    private static int GetPow(int digit)
    {
      int result = 1;
      for (int i = 0; i < digit; i++)
      {
        result *= 10;
      }
      return result;
    }
  }
}
